package cate.common.table.activity.hangdrop.common;

import cate.common.table.ERow;
import cate.common.table.XSSFRowWrap;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.AbstractMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@NoteClass("掉落兑换活动-基础配置 Row")
public class HangDropActivityCommonRow extends ERow {
    @NoteField("名称")
    public String name;
    @NoteField("分组")
    public int group;
    @NoteField("掉落间隔")
    public int dropInterval;
    @NoteField("掉落物品ID")
    public int dropItemId;
    @NoteField("掉落概率数量")
    public String dropNumConf;
    @NoteField("掉落天数")
    public int dropDay;
    @NoteField("过期物品兑换")
    public int overdueGoldRatio;
    @NoteField("邮件1")
    public int warnMailId;
    @NoteField("邮件2")
    public int endMailId;

    @NoteField("总权重")
    public int totalWeight;
    @NoteField("掉落数量配置")
    public List<Map.Entry<Integer, Integer>> dropNumList = new LinkedList<>();

    @NoteField("名称lang表ID")
    public int nameLangId;

    @Override
    public void parse(XSSFRowWrap xrw) {
        super.parse(xrw);
        id = xrw.getInteger("ID");
        name = xrw.getString("名称");
        group = xrw.getInteger("分组", 0);
        dropInterval = xrw.getInteger("掉落间隔");
        dropItemId = xrw.getInteger("掉落道具ID");
        dropNumConf = xrw.getString("掉落概率数量");
        dropDay = xrw.getInteger("掉落天数");
        overdueGoldRatio = xrw.getInteger("过期物品兑换", 0);
        warnMailId = xrw.getInteger("邮件1", 0);
        endMailId = xrw.getInteger("邮件2", 0);

        totalWeight = 0;
        try {
            String[] splits = dropNumConf.split("#");
            for (String oneStr : splits) {
                String[] params = oneStr.split(":");
                int num = Integer.parseInt(params[0]);
                int weight = Integer.parseInt(params[1]);
                dropNumList.add(new AbstractMap.SimpleEntry<>(num, weight));
                totalWeight += weight;
            }
        } catch (Exception e) {
            getContext().warningOnSheetLoading(parent.parent.getFileName(),
                    parent.getSheetName(),
                    "ID=" + id + " 掉落配置错误!");
        }

        if (dropInterval <= 0) {
            getContext().warningOnSheetLoading(parent.parent.getFileName(),
                    parent.getSheetName(),
                    "ID=" + id + " 掉落间隔配置必须大于0!");
            dropInterval = 9999;
        }

        try {
            nameLangId = Integer.parseInt(name);
        } catch (Exception e) {
            nameLangId = -1;
        }
    }

    /**
     * 获取掉落数量
     */
    public int getNum() {
        if (totalWeight == 0) {
            return 0;
        }
        int randWeight = (int) Math.floor(Math.random() * totalWeight);
        for (Map.Entry<Integer, Integer> pair : dropNumList) {
            if (randWeight < pair.getValue()) {
                return pair.getKey();
            } else {
                randWeight -= pair.getValue();
            }
        }
        return 0;
    }
}
